package com.taoyibao.hongcanting.widget;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.taoyibao.hongcanting.R;

/**
 * @author TangAnna
 * @description: RecyclerView SmartRefreshLayout EmptyView ErrorView 统一封装的
 * @date :${DATA} 15:35
 */
public class RefreshRecyclerView extends RelativeLayout {
    private SmartRefreshLayout mSmartRefreshLayout;
    private EmptyView mEmptyView;
    private RecyclerView mRecyclerView;
    private ErrorView mErrorView;

    public RefreshRecyclerView(Context context) {
        super(context);
    }

    public RefreshRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public RefreshRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        inflate(context, R.layout.refresh_recycler_view, this);
        mSmartRefreshLayout = findViewById(R.id.smartRefreshLayout);
        //设置 Header 为 Material风格
        mSmartRefreshLayout.setRefreshHeader(new ClassicsHeader(context));
        //设置 Footer 为 球脉冲
        mSmartRefreshLayout.setRefreshFooter(new ClassicsFooter(context));
        //设置当列表不能填充整个页面时不能加载更多
        mSmartRefreshLayout.setEnableLoadMoreWhenContentNotFull(false);
        mEmptyView = findViewById(R.id.emptyView);
        mRecyclerView = findViewById(R.id.dataView);
        //默认管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        mErrorView = findViewById(R.id.errorView);
    }

    /**
     * 设置空页面
     */
    public void setEmptyView() {
        mEmptyView.setVisibility(VISIBLE);
        mRecyclerView.setVisibility(GONE);
        mErrorView.setVisibility(GONE);
    }

    /**
     * 设置错误页面
     */
    public void setErrorView() {
        mEmptyView.setVisibility(GONE);
        mRecyclerView.setVisibility(GONE);
        mErrorView.setVisibility(VISIBLE);
    }

    /**
     * 设置数据页面
     */
    public void setDataView() {
        mEmptyView.setVisibility(GONE);
        mRecyclerView.setVisibility(VISIBLE);
        mErrorView.setVisibility(GONE);
    }

    public ErrorView getErrorView() {
        return mErrorView;
    }

    public SmartRefreshLayout getSmartRefreshLayout() {
        return mSmartRefreshLayout;
    }

    public EmptyView getEmptyView() {
        return mEmptyView;
    }

    public RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    /**
     * 刷新完成
     */
    public void setRefreshFinish() {
        mSmartRefreshLayout.finishRefresh();
        mSmartRefreshLayout.finishLoadMore();
    }
}
